import Upload from '@/components/common/Upload.vue';
import UploadFiles from '@/components/common/UploadFile.vue';
export default {
    components: {
        Upload,
        UploadFiles
    },
    name: 'SafeDialog',
    props: {
        safeFlag: Boolean,
        taskId: Number,
        taskName: String,
        workOrderNumber: String
    },
    data() {
        return {
            // 上传
            uploadHttp: '/api/attachment/uploadFiles',
            // 回显数组
            outRoom: [],
            // 附件上传
            fileUpload: '',
            a: 1,
            formObj: {
                pmsConstructionOrder: {},
                recordList: [],
                fileId: '',
                taskStatus: ''
            },
            fileList: [
                {
                    url: ''
                }
            ]
        };
    },
    created() {
        this.getInsideDetail();
    },
    methods: {
        // 上传成功
        uploadSuccess(response, file, fileList, item) {
            // 回显本地变量
            console.log('上传图片', response, file, fileList, item);
            let fileArray = this.formatFileListToArray(fileList);
            console.log(fileArray);
            // item.uploadImage = this.arrayFormatStr(fileArray)
            this.outRoom = fileList;
        },
        // 附件上传成功
        uploadSuccessfileUpload(response, file, fileList) {
            console.log('附件图片', response, file, fileList);
            let fileArray = this.formatFileListToArray(fileList);
            this.formObj.fileId = this.arrayFormatStr(fileArray);
            console.log(this.formObj.fileId);
        },
        //
        remove(file) {
            const index = this.outRoom.findIndex(img => img.uid === file.uid);
            this.outRoom.splice(index, 1);
        },
        getToken() {
            let result = localStorage.getItem('token');
            return result;
        },
        // 获取详情数据
        async getInsideDetail() {
            this.$httpExt()
                .post('/api/constructionTask/taskHandle', {
                    taskId: this.taskId,
                    taskName: this.taskName,
                    workOrderNumber: this.workOrderNumber
                })
                .then(
                    response => {
                        console.log('返回值', response);
                        this.formObj = response.data;
                    },
                    response => {
                        this.loading = false;
                        this.$message({
                            type: 'warning',
                            message: response.message
                        });
                    }
                )
                .catch(err => {
                    console.log(err);
                });
        },
        clonseDialog() {
            this.$emit('closeDialog');
        },
        saveDialog() {
            this.updateDialog('save');
            console.log('保存');
        },
        submitDialog() {
            console.log('提交');
            this.$confirm('确认提交？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.updateDialog('submit');
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        updateDialog(status) {
            this.$httpExt()
                .post('/api/constructionTask/saveOrUpdateData', {
                    taskId: this.taskId,
                    workOrderNumber: this.workOrderNumber,
                    fileIds: this.formObj.fileId,
                    constructionImageList: this.formObj.recordList,
                    taskName: this.taskName,
                    handStatus: status
                })
                .then(
                    response => {
                        console.log('操作成功', response);
                        this.$message({
                            type: 'info',
                            message: '操作成功'
                        });
                        this.clonseDialog();
                    },
                    response => {
                        console.log('操作成功', response);
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    }
                )
                .catch(err => {
                    console.log(err);
                });
        },
        formatFileListToArray(fileList) {
            let fileArray = [];
            fileList.forEach(file => {
                fileArray.push(file.response);
            });

            return fileArray;
        },
        /**
         * 将数组转成字符串
         * @param array
         */
        arrayFormatStr(array) {
            let str = '';
            if (array && array.length > 0) {
                for (let i = 0; i < array.length; i++) {
                    if (array[i] && array[i] === undefined) {
                        str += array[i];
                        //最后一个不加 ,
                        if (i !== array.length - 1) {
                            str += ',';
                        }
                    }
                }
            }
            return str;
        },
        transFileIdsToUrls(uploadImage) {
            let fileIdArray = uploadImage.split(',');
            let array = fileIdArray
                .filter(item => {
                    if (item) {
                        return true;
                    }
                })
                .map(fileId => {
                    return { response: fileId };
                });
            console.log('格式化后的》》》》》', array);
            return array;
        }
    }
};
